<?php 
class Admin_CategoryController extends Zend_Controller_Action{
	public function init(){
        $layoutPath = APP_PATH . '\templates\admin\\';
        
        $option = array ('layout'     => 'index',
                         'layoutPath' => $layoutPath,
                         'contentKey' => 'content' );
        Zend_Layout::startMvc( $option );
    }
	
	public function indexAction(){
		$cat = new Admin_Model_Category();
		$arrcat = $cat->getAllCategory();             
		$this->view->data = $arrcat;	
	}
	
    public function quickeditAction(){
        $id = $this->_request->getParam('id');
        $name = $this->_request->getParam('name');
        $des = $this->_request->getParam('des');
        $cat = new Admin_Model_Category();
        if($cat->quickEditCategory($name,$des,$id)){
            echo'1';
        }                  
        exit();        
    }
    
	public function addAction(){
		$cat = new Admin_Model_Category();
		//$cat->addCategory('bai1','asd','hsfsdg');
		if($this->_request->isPost()){
			$catname = $this->_request->getParam('txtCatName');
            $catdes  = $this->_request->getParam('txtDes');
            //$catimg  = $this->_request->getParam('txtFile');
			//upload image
			$file     = $_FILES['txtFile']; 
			$url      = APP_PATH.'/templates/admin/images/upload/category/';
			$arrAllowEx=array('jpg','jpeg','png','gif','bmp');
			$maxSize  =5242880;
			$catimg    =$cat->uploadFile($file,$url,$arrAllowEx,$maxSize);
			$err = '';
			$i = 1;
			$data=array();
			if($catname==''){
				$err.=$i.' Catname must be not null.</br>';
                $i++;
			}else{
				$data['cat_name']=$catname;
			}
			if($catdes==''){
				$err.=$i.' Catname must be not null.</br>';
                $i++;
			}else{
				$data['cat_des']=$catdes;
			}
			if($catimg==''){
				$err.=$i.' Catname must be not null.</br>';
                $i++;
			}else{
				$data['cat_des']=$catimg;
			}
			
			if($err!=''){
                $this->view->err = $err;
                $this->view->cat = $data;
             }else{
                $check = $cat->addCategory($catname,$catdes,$catimg);
                if($check){
                    $this->_redirect('/admin/category/index');
                }
             }
			
		}
	}
	
	public function editAction(){
		$cat = new Admin_Model_Category();
		$id  = $this->_request->getParam('id');
        $category = $cat->getCategory($id);;
        
        $catname = $this->_request->getParam('txtCatName');   		
        $catdes = $this->_request->getParam('txtCatDes');       
        $catimgnew = @$this->_request->getParam('attach');
         
		if($this->_request->isPost()){ 
			
            $err='';
            $i=1;
             
             if($catname==''){
                $err.=$i.' Catname be not null.</br>';
                $data['cat_name']='';
                $i++;
             }else{
                $data['cat_name']=$catname;
             }
			 if($catdes==''){
                $err.=$i.' Catdes be not null.</br>';
                $data['cat_des']='';
                $i++;
             }else{
                $data['cat_des']=$catdes;
             }
             
             
             if($err!=''){
                $data['cat_img'] = $category['cat_img'];
                $this->view->err = $err;
                $this->view->cat = $data;
             }else{
                // neu khong chon anh moi thi lay anh cu  
    			 if($catimgnew=='-1'){
                    $catimg=$category['cat_img'];
                 }else{  // neu chon anh moi thi xoa anh cu khoi server
                    $catimg=$catimgnew;
                    $url = APP_PATH.'/templates/admin/images/upload/category/';
                    
                    unlink($url.$category['cat_img']);
                 }
                $check = $cat->editCategory($catname,$catdes,$catimg,$id);
                if($check){
                    $this->_redirect('/admin/category/index');
                }
             }
             
		}else{
		      $this->view->cat = $cat->getCategory($id);
		}
	}
	
	public function deleteAction(){
		$cat = new Admin_Model_Category();
        $id = $this->_request->getParam('id');
		$check = $cat ->delete('cat_id IN('.$id.')');
		if($check){
			$this->_redirect('/admin/category/index');
		}
	}
    
    public function getExtendtion($str){
		$extend="";
		$posdot=strrpos($str,'.');
		$extend=substr($str,$posdot+1);
		return $extend;
	}
    
    public function uploadAction(){
        $url = '/templates/admin/images/upload/category/';
        
        $allowFileType = array('jpg','jpeg','png','gif','bmp');        
        $realpath = APP_PATH.$url;
        $basepath = substr($_SERVER['PHP_SELF'],0,strpos ($_SERVER['PHP_SELF'],'/',1)).'/app'.$url;              
       
        $file = $_FILES['imgfile'];
        $fName = $file['name'];
        $fSize = $file['size'];
        $ftmpName=$file['tmp_name']; 
        $maxSize = 5242880;
        $fExtend=strtolower($this->getExtendtion($fName));
        
        if(!in_array($fExtend,$allowFileType)){
			echo '0';
		}            
		elseif($fSize>$maxSize){
			echo'-1';			
		}else{
            $Name=str_replace(' ','-',$fName);
            $Name=time().'_'.$fName;		
			if(move_uploaded_file($ftmpName,$realpath.$Name)){
				chmod($realpath.$Name,0776);
				$return=$basepath.$Name;
			}            
            $return.='@'.$Name;
            echo $return;
		}
		exit();
    }
    
    public function delimgAction(){
        $url = APP_PATH.'/templates/admin/images/upload/category/';
        $imgolder = $this->_request->getParam('img');
        unlink($url.$imgolder);
        exit;
    }
}